#!/usr/bin/python3
import cherrypy
import requests
import cherrypy.lib
import os

class Avatar():
    bs_avatar = "https://id.bayan.ir/bayan/profile/avatar/1yHznvLMst4f4LgQDsdonXAAN98%3D/"
    fkz_avatar = "https://id.bayan.ir/bayan/profile/avatar/n3fsHgxS0NoH2sYh2uNRsDK57cc%3D/"
    @cherrypy.expose
    def index(self, q=""):
        if not q:
            return """
            <html><body>Hello. Send data to this address as q</body></html>
            """
        
        if "BS" in q:
            buf = requests.get(self.bs_avatar, stream=True).raw
        elif "فاروق" in q:
            buf = requests.get(self.fkz_avatar, stream=True).raw
        else:
            buf = open("unknown.jpg", "rb")
        cherrypy.response.headers["Content-type"] = "image/jpg"
        return cherrypy.lib.file_generator(buf)
if __name__ == "__main__":
    conf = {
        "/": {
            "tools.staticdir.root": os.path.abspath(os.getcwd()),
        },
    }
    cherrypy.config.update({"server.socket_host": "0.0.0.0"})
    cherrypy.config.update({"server.socket_port": 2052})
    cherrypy.quickstart(Avatar(), "/", conf)
